package org.example.struct.array2;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author liyishan
 * @date 2024/9/10 10:14
 * @apiNote
 */

public class Test02841MaxSum {
    public long maxSum(List<Integer> nums, int m, int k) {
        long sum = 0;
        Map<Integer,Integer> counts = new HashMap<Integer,Integer>();
        int n = nums.size();
        for(int i = 0;i<k;i++){
            int num = nums.get(i);
            sum += num;
            counts.put(num, counts.getOrDefault(num,0)+1);
        }
        long maximumSum = counts.size() >= m ? sum:0;
        for(int i = k;i<n;i++){
            int prevNum = nums.get(i-k),currNum = nums.get(i);
            sum -= prevNum;
            sum += currNum;
            counts.put(prevNum,counts.get(prevNum)-1);
            if(counts.get(prevNum) == 0){
                counts.remove(prevNum);
            }
            counts.put(currNum,counts.getOrDefault(currNum,0)+1);
            if(counts.size() >= m){
                maximumSum = Math.max(maximumSum,sum);
            }
        }
        return maximumSum;
    }
}
